package com.mkl.business.controller;

import com.mkl.business.domain.Customer;
import com.mkl.business.service.CustomerService;
import com.mkl.business.vo.CustomerVo;
import com.mkl.system.common.Constant;
import com.mkl.system.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/3 13:08
 */
@RestController
@RequestMapping("api/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 查询全部客户
     *
     * @param customerVo 条件vo
     * @return 返回数据
     */
    @RequestMapping("loadAllCustomer")
    public Object loadAllCustomer(CustomerVo customerVo) {
        return customerService.loadAllCustomer(customerVo);
    }

    /**
     * 新增客户
     *
     * @param customer 客户
     * @return 返回结果
     */
    @RequestMapping("addCustomer")
    public ResultObj addCustomer(Customer customer) {
        try {
            customer.setAvailable(Constant.AVAILABLE_TRUE);
            customerService.saveCustomer(customer);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新客户
     *
     * @param customer 客户
     * @return 返回结果
     */
    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(Customer customer) {
        try {
            customerService.updateCustomer(customer);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据id删除数据
     *
     * @param id 客户id
     * @return 返回结果
     */
    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomerById(Integer id) {
        try {
            customerService.deleteCustomerById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("batchDeleteCustomer")
    public ResultObj batchDeleteCustomerByIds(Integer[] ids) {
        try {
            if (ids != null && ids.length > 0) {
                customerService.batchDeleteCustomerByIds(ids);
                return ResultObj.DELETE_SUCCESS;
            } else {
                return new ResultObj(-1, "删除数据不能为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 查询所有可用的客户
     *
     * @return 结果
     */
    @GetMapping("getAllAvailableCustomer")
    public Object getAllAvailableCustomer() {
        return customerService.getAllAvailableCustomer();
    }

    /**
     * 查询所有可用的客户总数
     *
     * @return 结果
     */
    @GetMapping("getAllAvailableCustomerSum")
    public Integer getAllAvailableCustomerSum() {
        return customerService.getAllAvailableCustomerSum();
    }
}
